#include<iostream>
#include<cassert>
#include<cstring>
using namespace std;

namespace code 
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		//1.默认成员函数
		//构造
		vector() {}

		vector(const T& val)
		{
			push_back(val);
		}

		vector(size_t n, const T& val = T()) 
		{
			resize(n,val);
		}

		vector(int n, const T& val = T())
		{
			resize(n, val);
		}

		//迭代器区间构造
		template<class InputIterator>
		vector(InputIterator first, InputIterator last) 
		{
			while (first != last) 
			{
				push_back(*first);
				first++;
			}
		}

		//拷贝
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto val : v)
				push_back(val);
		}
		//赋值
		void swap(vector<T>& v) 
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}


		//析构
		~vector() 
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		//修改
		void push_back(const T& val = T()) 
		{
			if (_finish == _end_of_storage)
					reserve(capacity() == 0 ? 4 : 2*capacity());
			*_finish = val;
			_finish++;
		}

		void pop_back() 
		{
			assert(size() > 0);
			_finish--;
		}

		void insert(iterator pos, const T& val) 
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			//扩容检查
			if (_finish == _end_of_storage) 
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator it = _finish - 1;
			while (it >= pos) 
			{
				*(it + 1) = *it;
				--it;
			}
			*pos = val;
			++_finish;
		}


		iterator erase(iterator pos)
		{
			assert(size() > 0);
			//挪动数据
			iterator cur = pos;
			while (cur < _finish -1) 
			{
				*cur = *(cur+1);
				cur++;
			}
			_finish--;
			return pos;
		}


		//容量
		size_t size()const { return _finish - _start; }
		size_t capacity()const { return _end_of_storage - _start; }
		void reserve(size_t n) 
		{
			if (n > capacity()) 
			{
				T* tmp = new T[n];
				size_t old_size = size();

				//处理旧数据
				for (size_t i = 0; i < old_size; i++)
					tmp[i] = _start[i];
				delete[]_start;

				//更新数据
				_start = tmp;
				_finish = _start + old_size;
				_end_of_storage = _start + n;
			}
		}

		void resize(size_t n, const T& val = T()) 
		{
			if (n < size()) 
			{
				_finish = _start + n;
			}
			else if(n > size())
			{
				if (n > capacity()) 
				{
					reserve(n);
					for (size_t i = size(); i < n; i++) 
					{
						*_finish = val;
						++_finish;
					}
				}
			}
		}

		//访问
		//1.[]
		T& operator[](size_t pos) 
		{
			assert(pos < size());
			return *(_start + pos);
		}
		//2.迭代器
		iterator begin() { return _start; }
		iterator end() { return _finish; }
		const_iterator begin()const { return _start; }
		const_iterator end()const { return _finish; }

		//其它
		void print() 
		{
			for (size_t i = 0; i < size(); i++)
				cout << _start[i] << " ";
			cout << endl;
		}	

	private:
		T* _start = nullptr;
		T* _finish = nullptr;
		T* _end_of_storage = nullptr;
	};

	void test1() 
	{
		cout << "默认成员函数" << endl;
		vector<int> v1;
		cout << "v1(): "; v1.print();

		vector<int> v2(10,1);
		cout << "v2(10,1): "; v2.print();

		vector<int> v3(v2);
		cout << "v3(v2): "; v3.print();

		v1 = v3;
		cout << "v1 = v3: "; v1.print();

		vector<int> v4(v1.begin() + 1, v1.end()-1);
		cout << "v4(v1.begin() + 1, v1.end() - 1): "; v4.print();
	}

	void test2() 
	{
		cout << "容量" << endl;
		vector<int> v1(10,1);
		cout << "v1(10,1): size: " << v1.size() << " capacity: " << v1.capacity() << endl;
		v1.reserve(20);
		cout << "v1.reserve(20): size: " << v1.size() << " capacity: " << v1.capacity() << endl;
		v1.resize(5);
		cout << "v1.resize(5): size: " << v1.size() << " capacity: " << v1.capacity() << endl;

	}

	void test3() 
	{
		cout << "访问" << endl;
		vector<int> v1(10,1);

		cout << "[]访问: v1(10,1);";
		for (int i = 0; i < v1.size(); i++) 
		{
			cout << v1[i] << " ";
		}
		cout << endl;

		cout << "迭代器访问: v1(10,1);";
		auto it = v1.begin();
		while (it != v1.end()) 
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void test4() 
	{
		cout << "modify" << endl;
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		cout << "v.push_back(): "; v.print();

		v.insert(v.begin(),3);
		v.insert(v.begin(),4);
		v.insert(v.begin(),5);
		cout << "v.insert(): begin() "; v.print();

		v.erase(v.begin());
		cout << "v.erase: begin()"; v.print();

		v.pop_back();
		cout << "v.pop_back()"; v.print();

	}
}